<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // 向右移动之后在向下移动 
        // 封装的形式：
        let boxEle = document.querySelector(".box");
        function move(boxEle, target, dir, cb) {
            return new Promise(resolve => {
            var timer = setInterval(() => {
                let num = parseInt(getComputedStyle(boxEle, null)[dir]);
                // currentStyle  :IE 浏览器
                    let speed = 10 * (target > num ? 1 : -1);
                    if (num === target) {
                        clearInterval(timer);
                        cb && cb();
                        resolve()
                    } else {
                        boxEle.style[dir] = num + speed + "px";
                    }
                }, 100)
            })
        }

        // 画圈  
        // 回调地狱 ；
        // 如何解决回调地狱 ；
        // 1.观察者模式 ；2.promise
        move(boxEle, 300,"left").then(()=>{
            return move(boxEle, 300, "top")
        }).then(()=>{
            return move(boxEle, 0, "left")
        }).then(()=>{
            return move(boxEle, 0, "top")
        }).then(()=>{})


        // move(boxEle, 300, "left", function () {
        //     move(boxEle, 300, "top", function () {
        //         move(boxEle, 0, "left", function () {
        //             move(boxEle, 0, "top", function () {
        //             })
        //         })
        //     })
        // })
    </script>
</body>

</html>